/*
 *所有关于OutSourcingWareHousing类的业务代码应在此处编写
*可使用repository.调用常用方法，获取EF/Dapper等信息
*如果需要事务请使用repository.DbContextBeginTransaction
*也可使用DBServerProvider.手动获取数据库相关信息
*用户信息、权限、角色等使用UserContext.Current操作
*OutSourcingWareHousingService对增、删、改查、导入、导出、审核业务代码扩展参照ServiceFunFilter
*/
using VOL.Core.BaseProvider;
using VOL.Core.Extensions.AutofacManager;
using VOL.Entity.DomainModels;
using System.Linq;
using VOL.Core.Utilities;
using System.Linq.Expressions;
using VOL.Core.Extensions;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Http;
using MyMES.IRepositories;
using MyMES.Dto;
using MyMes.Infrastructure;
using MyMES.Infrastructure;

namespace MyMES.Services
{
    public partial class OutSourcingWareHousingService
    {
        private readonly IBaseRepository<OutSourcingWareHousing> _outSourcingWareHousingRepository;

        public OutSourcingWareHousingService(IBaseRepository<OutSourcingWareHousing> outSourcingWareHousingRepository)
        {
            _outSourcingWareHousingRepository = outSourcingWareHousingRepository;
        }

        /// <summary>
        /// 获取外协入库信息
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public async Task<APIPage<OutSourcingWareHousing>> GetOutSourcingWareHousingList(GetOutSourcingWareHousingListDTO param)
        {
            var list = _outSourcingWareHousingRepository.GetAll();

            if (!string.IsNullOrEmpty(param.StockroomCode))
            {
                list = list.Where(e => e.StockroomCode.Contains(param.StockroomCode));
            }

            if (!string.IsNullOrEmpty(param.StockroomName))
            {
                list = list.Where(e => e.StockroomName.Contains(param.StockroomName));
            }

            if (!string.IsNullOrEmpty(param.SupplierName))
            {
                list = list.Where(e => e.SupplierName == param.SupplierName);
            }

            var supplierpaging = new APIPage<OutSourcingWareHousing>()
            {
                Data = list.Skip((param.PageIndex - 1) * param.PageSize).Take(param.PageSize).ToList(),
                TotalCount = list.Count(),
                TotalPage = (int)Math.Ceiling(list.Count() * 1.0 / param.PageSize)
            };

            return supplierpaging;
        }

        /// <summary>
        /// 新增外协入库
        /// </summary>
        /// <param name="Salesreturns"></param>
        /// <returns></returns>
        public async Task<int> CreateOutSourcingWareHousing(OutSourcingWareHousing salesreturns)
        {
            var result = await _outSourcingWareHousingRepository.AddAsync(salesreturns);

            return result;
        }

        /// <summary>
        /// 删除外协入库信息
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public async Task<int> DeleteOutSourcingWareHousing(OutSourcingWareHousing salesreturns)
        {
            var result = await _outSourcingWareHousingRepository.Remove(salesreturns);

            return result;
        }

        /// <summary>
        /// 修改外协入库信息
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public async Task<int> UpdateOutSourcingWareHousing(OutSourcingWareHousing salesreturns)
        {
            var result = await _outSourcingWareHousingRepository.UpdateAsync(salesreturns);

            return result;
        }

        /// <summary>
        /// 执行入库
        /// </summary>
        /// <param name="id"></param>
        /// <param name="isDocument"></param>
        /// <returns></returns>
        public async Task<int> ExecuteWarehousing(OutSourcingWareHousing salesreturns)
        {
            salesreturns.IsDocument = 2;

            var query = await _outSourcingWareHousingRepository.UpdateAsync(salesreturns);

            return query;
        }
    }
}
